package com.zeji.androidadvance.activity.handler;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.zeji.androidadvance.R;
import com.zeji.androidadvance.utils.LogUtil;

@SuppressLint("NonConstantResourceId")
public class HandlerTestActivity extends AppCompatActivity {
    private final static int MSG_WHAT_UPDATE_TIME = 1001;

    @BindView(R.id.handlerTestTime)
    TextView tvTime;

    TimerThread timerThread;
    Handler timeUpdateHandler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler_test);
        ButterKnife.bind(this);

        initHandler();
    }

    private void initHandler() {
        timeUpdateHandler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                if (msg.what == MSG_WHAT_UPDATE_TIME) {
                    tvTime.setText(String.valueOf(msg.arg1));
                }
            }
        };
    }

    @OnClick({R.id.handlerTestStart, R.id.handlerTestEnd})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.handlerTestStart: {
                startTimer();
                break;
            }
            case R.id.handlerTestEnd: {
                endTimer();
                break;
            }
        }
    }

    private void startTimer() {
        endTimer();
//        new Thread(){
//            @Override
//            public void run() {
//                super.run();
//            }
//        }.start();
        timerThread = new TimerThread(timeUpdateHandler);
        timerThread.start();
    }

    private void endTimer() {
        if (timerThread != null) {
            timerThread.interrupt();
            timerThread = null;
        }
    }

    @Override
    protected void onDestroy() {
        endTimer();
        super.onDestroy();
    }

    static class TimerThread extends Thread {
        TimerHandler timerHandler1;
        final Handler updateHandler;

        public TimerThread(Handler updateHandler) {
            super();
            this.updateHandler = updateHandler;
        }

        @Override
        public void run() {
            super.run();

            Looper.prepare();
            timerHandler1 = new TimerHandler(Looper.myLooper(), updateHandler);
            Message message = new Message();
            message.what = TimerHandler.MSG_UPDATE;
            message.arg1 = 1;
            timerHandler1.sendMessage(message);
            Looper.loop();
        }

        @Override
        public void interrupt() {
            super.interrupt();
            if (timerHandler1 != null) {
                Message message = new Message();
                message.what = TimerHandler.MSG_STOP;
                timerHandler1.sendMessage(message);
            }
            timerHandler1 = null;
        }
    }

    static class TimerHandler extends Handler {
        private static final int MSG_STOP = -1;
        private static final int MSG_UPDATE = 1;

        Handler updateHandler;
        volatile int nowValue = 0;

        public TimerHandler(@NonNull Looper looper, Handler updateHandler) {
            super(looper);
            this.updateHandler = updateHandler;
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_STOP: {
                    removeMessages(MSG_UPDATE);
                    updateHandler = null;
                    break;
                }
                case MSG_UPDATE: {
                    if (nowValue < 0) {
                        return;
                    }
                    nowValue = msg.arg1;
                    if (updateHandler != null) {
                        Message message = new Message();
                        message.what = MSG_WHAT_UPDATE_TIME;
                        message.arg1 = nowValue;
                        LogUtil.d("更新时间：", nowValue);
                        updateHandler.sendMessage(message);
                    }
                    Message message = new Message();
                    message.what = MSG_UPDATE;
                    message.arg1 = nowValue + 1;
                    LogUtil.d("通知下一个：", nowValue + 1);
                    sendMessageDelayed(message, 1000);
                    break;
                }
            }
        }
    }
}
